<template>
  <div class="member-order-page">
    <!-- tab组件 -->
    <XtxTabs v-model="activeName" @tab-click="clickTab">
  <XtxTabsPanel
        v-for="item in orderStatus"
        :key="item.name"
        :label="item.label"
        :name="item.name"
        ></XtxTabsPanel>
    </XtxTabs>
    <!-- 列表 -->
    <div class="order-list">
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem @on-cancel="handlerCancel" @on-delete="handlerDelete" @on-confirm="handlerConfirm" @on-logistics="handlerLogistics" v-for="item in orderList" :key="item.id" :order="item"></OrderItem>
    </div>
    <!-- 分页 -->
    <XtxPagination :current-page="reqParams.page" :page-size="reqParams.pageSize" :total="total" @current-change="reqParams.page=$event"></XtxPagination>
    <!-- 取消原因 -->
    <OrderCancel ref="orderCancelCom"></OrderCancel>
    <!-- 物流 -->
    <OrderLogistics ref="orderLogisticsCom"></OrderLogistics>
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { orderStatus } from '@/api/constants'
import OrderItem from './components/order-item.vue'
import OrderCancel from './components/order-cancel.vue'
import OrderLogistics from './components/order-logistics.vue'
import { confirmOrder, deleteOrder, findOrderList } from '@/api/order'
import Confirm from '@/components/library/Confirm'
import Message from '@/components/library/Message'
export default {
  name: 'MemberOrder',
  components: { OrderItem, OrderCancel, OrderLogistics },
  setup () {
    const activeName = ref('all') // 动态激活 默认激活全部
    const orderList = ref([]) // 订单列表
    const loading = ref(false)
    const total = ref(0)
    // 筛选条件
    const reqParams = ref({
      page: 1,
      pageSize: 5,
      orderState: 0
    })
    // 渲染订单列表
    function getOrderList () {
      loading.value = true
      findOrderList(reqParams.value).then(data => {
        orderList.value = data.result.items
        total.value = data.result.counts
        loading.value = false
      })
    }
    // 筛选条件变化重新渲染
    watch(reqParams.value, () => {
      getOrderList()
    }, { immediate: true })
    // 切换tab
    const clickTab = ({ index }) => {
      reqParams.value.page = 1
      reqParams.value.orderState = index
    }
    // 删除订单
    function handlerDelete (order) {
      Confirm({ text: '确认删除？' }).then(() => {
        deleteOrder(order.id).then(() => {
          Message({ type: 'success', text: '删除成功' })
          getOrderList()
        })
      }).catch(e => {})
    }
    return {
      activeName,
      clickTab,
      orderStatus,
      orderList,
      loading,
      total,
      reqParams,
      handlerDelete,
      ...useCancel(),
      ...useConfirm(),
      ...useLogistics()
    }
  }
}
// 取消订单
export function useCancel () {
  const orderCancelCom = ref(null)
  function handlerCancel (order) {
    orderCancelCom.value.open(order)
  }
  return { handlerCancel, orderCancelCom }
}
// 确认收货
export function useConfirm () {
  function handlerConfirm (order) {
    Confirm({ text: '确认收货？' }).then(() => {
      confirmOrder(order.id).then(() => {
        Message({ type: 'success', text: '确认收货成功' })
        // 更新订单状态
        order.orderState = 4
      })
    }).catch(e => {})
  }
  return { handlerConfirm }
}
// 查看物流
export function useLogistics () {
  const orderLogisticsCom = ref(null)
  function handlerLogistics (order) {
    orderLogisticsCom.value.open(order)
  }
  return { handlerLogistics, orderLogisticsCom }
}
</script>
<style scoped lang="less">
.member-order-page{
  height: 100%;
  background: #fff;
}
.order-list {
  padding: 20px;
  position: relative;
  min-height: 400px;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255,255,255,.9) url(../../../assets/images/loading.gif) no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
